//
//  NewsVc.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2019/3/8.
//  Copyright © 2019 Gox_WallyDiao. All rights reserved.
//

import UIKit

class NewsVc: TTBaseVC {
    let SystemNewsTableViewCellID = "SystemNewsTableViewCell"
    let SystemTimeNewsTableViewCellID = "SystemTimeNewsTableViewCell"
    let ActivityNewsTableViewCellID = "ActivityNewsTableViewCell"
    let CommentNewsTableViewCellID = "CommentNewsTableViewCell"
    
    var type:Int = 0
    
    var newsModel: [NewsModel] = [NewsModel]()
    
    private lazy var tableView: TTTableView = {
        let tableView = TTTableView.init(frame: FRAME_CGFloat(0, 44, SCREEN_WIDTH, SCREEN_HEIGHT - OFFSET_TOP - 57), style: UITableView.Style.plain)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = WhiteBackColor
        tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
        tableView.register(SystemNewsTableViewCell.self, forCellReuseIdentifier: SystemNewsTableViewCellID)
        tableView.register(SystemTimeNewsTableViewCell.self, forCellReuseIdentifier: SystemTimeNewsTableViewCellID)
        tableView.register(ActivityNewsTableViewCell.self, forCellReuseIdentifier: ActivityNewsTableViewCellID)
        tableView.register(CommentNewsTableViewCell.self, forCellReuseIdentifier: CommentNewsTableViewCellID)
        tableView.tableFooterView = UIView.init(frame: .zero)
        //        tableView.tableHeaderView = UIView.init(frame: FRAME_CGFloat(0, 0, SCREEN_WIDTH, 8))
        return tableView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "消息中心"
        
        let news = NewsHeaderView.init(frame: FRAME_CGFloat(0, 0, SCREEN_WIDTH, 44))
        news.dateBlock = { (num) in
            switch num {
            case 11:
                self.type = 0
                self.setData()
            case 12:
                self.type = 1
                self.setData()
            case 13:
                self.type = 3
                self.setData()
                TTAlertView.showMessage(message: "暂未开放，敬请期待！", second: 2)
            default:
                return
            }
            
        }
        self.view.addSubview(news)
        
        self.view.addSubview(tableView)
        tableView.backgroundColor = UIColor.clear
        
        setData()
    }
    
}

//  MARK: - ###### 方法 ######
extension NewsVc {
    func setData() {
        self.newsModel.removeAll()
        MTProgressHUD.show()
        MineRequestModel.loadNews(parameters: ["userId":String.getUserId(),"type":self.type,"startPoint":1,"maxPage":999]) { (success, message, model) in
            if success && model != nil {
                self.newsModel = model!
                
            }
            self.tableView.reloadData()
            if self.newsModel.count > 0 {
                self.tableView.setNoData(nodata: false, frame: FRAME_CGFloat(SCREEN_WIDTH / 2 - 83.5, 100, 167, 167), image: "placeholderImage1", des: "暂时没有消息哦~")
            } else {
                self.tableView.setNoData(nodata: true, frame: FRAME_CGFloat(SCREEN_WIDTH / 2 - 83.5, 100, 167, 167), image: "placeholderImage1", des: "暂时没有消息哦~")
            }
            MTProgressHUD.hidded(true)
        }
    }
    
}

//  MARK: - ###### 协议 ######
extension NewsVc: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.newsModel.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        switch self.type {
        case 0:
            if self.newsModel[indexPath.row].dataType == 0 {
                return 30
            } else {
                return 85.5
            }
        case 1:
            if self.newsModel[indexPath.row].dataType == 0 {
                return 30
            } else {
                return 111 + SCREEN_SCALE_X(x: 137)
            }
        case 2:
            return 0
        default:
            return 0
        }
        
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        switch self.type {
        case 0:
            if self.newsModel[indexPath.row].dataType == 0 {
                let cell = tableView.dequeueReusableCell(withIdentifier: SystemTimeNewsTableViewCellID) as! SystemTimeNewsTableViewCell
                cell.backgroundColor = UIColor.clear
                cell.selectionStyle = .none
                cell.timeLabel.text = self.newsModel[indexPath.row].createTimeStr
                return cell
            } else {
                let cell = tableView.dequeueReusableCell(withIdentifier: SystemNewsTableViewCellID) as! SystemNewsTableViewCell
                cell.backgroundColor = UIColor.clear
                cell.selectionStyle = .none
                cell.leftImage.sd_setImage(with: URL(string:(StringIncludeChineseToString(self.newsModel[indexPath.row].imageUrl ?? ""))), placeholderImage: IMAGE(name: "welfarePlace"), options: .lowPriority, completed: nil)
                cell.titleLabel.text = self.newsModel[indexPath.row].title
                cell.describeLabel.text = self.newsModel[indexPath.row].content
                cell.timeLabel.text = self.newsModel[indexPath.row].createTimeStr
                return cell
            }
            
        case 1:
            if self.newsModel[indexPath.row].dataType == 0 {
                let cell = tableView.dequeueReusableCell(withIdentifier: SystemTimeNewsTableViewCellID) as! SystemTimeNewsTableViewCell
                cell.backgroundColor = UIColor.clear
                cell.selectionStyle = .none
                cell.timeLabel.text = self.newsModel[indexPath.row].createTimeStr
                return cell
            } else {
                let cell = tableView.dequeueReusableCell(withIdentifier: ActivityNewsTableViewCellID) as! ActivityNewsTableViewCell
                cell.backgroundColor = UIColor.clear
                cell.selectionStyle = .none
                cell.backImage.sd_setImage(with: URL(string:(StringIncludeChineseToString(self.newsModel[indexPath.row].imageUrl ?? ""))), placeholderImage: IMAGE(name: "welfarePlace"), options: .lowPriority, completed: nil)
                cell.titleLabel.text = self.newsModel[indexPath.row].title
                cell.describeLabel.text = self.newsModel[indexPath.row].content
                return cell
            }
            
        case 2:
            return UITableViewCell.init()
        default:
            return UITableViewCell.init()
        }
        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if self.newsModel[indexPath.row].dataType == 0 {
        } else {
            switch self.newsModel[indexPath.row].sysMsgType {
            case 0:
                let playVc = TradingRecordVc()
                PUSHVIEWCONTROLLER(playVc)
            case 1:
                let vc = TBWithdrawVc()
                PUSHVIEWCONTROLLER(vc)
            case 2:
                let vc = SignRedVc()
                PUSHVIEWCONTROLLER(vc)
            case 3:
                print("全站免费卡，使用后全站可以收听（等该功能做好后再上线）")
            case 4:
                let mainVC = TTTabBarController()
                AppDelegate.getAppDelegate().window?.rootViewController = mainVC
                mainVC.selectedIndex = 2
            case 5:
                print("栏目详情页")
            case 6:
                let mainVC = TTTabBarController()
                AppDelegate.getAppDelegate().window?.rootViewController = mainVC
                mainVC.selectedIndex = 0
            case 7:
                print("我的订单/待付款（订单中没有该状态就后续优化添加）")
            default:
                print("其他")
            }
        }
    }

}
